var Robot = require("./Robot");
var schedule = require("node-schedule");
var stockChannelCtrl = require("../controller/controller.stockChannel");

exports.init = function (){
    var that = this;
    stockChannelCtrl.findAllStock(null,function(err,result){
        var url = "http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/stockParam.phtml?year=yearParam&jidu=jiduParam";
        var urls = [];
        var years = [new Date().getFullYear(),new Date().getFullYear()-1];
        var jidus = [1,2,3,4];
        for(item of result.obj){
            var stock = item.stockCode;
            for(year of years){
                for (jidu of jidus){
                    urls.push(url.replace("stockParam",stock.substr(0,stock.indexOf("."))).replace("yearParam",year).replace("jiduParam",jidu));
                }
            }

            //console.log(urls);
        }
        that.go(urls);
        //options.firstUrl = result.obj;
    });
}
exports.go = function (urls) {

    var options = {
        domain: "money.finance.sina.com.cn",
        firstUrl: urls||[],//"http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/601005.phtml?year=2018&jidu=1"
        outputPath: "./output/testRobot/",
        outputFileName: "stock.txt",
        encoding: "GBK",
        timeout: 6000,
        robots: false,
        debug: true,
        handlerComplete: function (oResult, file) {

            console.log("抓取结束...");


           // file.save("\n抓取完成...\n总共访问网页数为" + oResult.iCount + "条，其中成功访问网页数" + oResult.iSuccessNum + "条", true);

        }
    };


    var robot = new Robot(options);
    var reg1 = /\/html\/[a-z0-9]+\/[a-z0-9]+\/[\d]+\/[\d]+\.html/gmi;
    var reg2 = /\/html\/[a-z0-9]+\/index\.html/gmi;


    function start() {

        robot.go(function ($, aType, url, aNewURLQueue, aTargetURLList, oTargetInfoList) {

            var self = this;
            var pUrl = url;
            if (url === options.firstUrl) {

                var aA = $("a");

                var href = url;
                var res = reg1.exec(href);

                aNewURLQueue.indexOf(href) == -1 && aNewURLQueue.push(href);


            }

        });
    }


    var rule = new schedule.RecurrenceRule();

    rule.dayOfWeek = [0, new schedule.Range(1, 6)];

    rule.hour = 07;

    rule.minute = 05;
    // rule.hour = 13;
    //
    // rule.minute = 57;

    console.log("定时爬取任务，下次爬取时间为" + rule.hour + "时" + rule.minute + "分");

 var j = schedule.scheduleJob(rule,function(){

     // robot.setOpt({
     //     outputFileName: getTime() + "-" + "stock.txt"
     // });
    console.log("开始定时爬取任务...");
    start();
 });
};
// robot end